home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3095 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  118 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: help w/ operator overloading
  5. Date: 22 Jan 1996 08:14:17 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan22091418@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4dq6ni$nrq@frodo.smartlink.net>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: thomas@smartlink.net's message of 20 Jan 1996 07:48:02 GMT
  12.  
  13. In article <4dq6ni$nrq@frodo.smartlink.net> thomas@smartlink.net (tom) writes:
  14.  
  15.    Help,
  16.    Anyone know why I don't get into my XPoint + operator when adding the classes
  17.    together? The Z works but not the X & Y.
  18.  
  19.    Thanks
  20.    ==============================================================================
  21.  
  22.    #include <stdio.h>
  23.  
  24.    class XPoint {
  25.    private:
  26.        double x,y;
  27.  
  28.    public:
  29.        double & X(){ return x;}
  30.        double & Y(){ return y;}
  31.  
  32.        XPoint(double _x, double _y):x(_x),y(_y){}
  33.        XPoint():x(0.0),y(0.0){}
  34.  
  35.        virtual XPoint operator+( XPoint &pt){
  36.            x = x + pt.X();
  37.            y = y + pt.Y();
  38.            return *this;
  39.        }
  40.  
  41.        virtual XPoint operator=( XPoint &pt){
  42.            x =  pt.X();
  43.            y =  pt.Y();
  44.            return *this;
  45.        }
  46.  
  47.    };
  48.  
  49.  
  50.    class XPoint3D: virtual public XPoint{
  51.    private:
  52.        double z;
  53.    public:
  54.        double &Z(){ return z;}
  55.        XPoint3D(double _x, double _y, double _z):XPoint(_x,_y),z(_z){}
  56.        XPoint3D():XPoint(),z(0.0){}
  57.  
  58.        XPoint3D operator+( XPoint3D &pt){
  59.            Z() = Z() + pt.Z();
  60.            return *this;
  61.        }
  62.        XPoint3D operator=( XPoint3D &pt){
  63.            Z() = pt.Z();
  64.            return *this;
  65.        }
  66.    };
  67.  
  68.  
  69.  
  70.    void main(){
  71.  
  72.        XPoint3D a;
  73.        XPoint3D b(10,10,10);
  74.        XPoint3D c(10,10,10);
  75.  
  76.        printf( "a= (%f,%f)\n",a.X(),a.Y() );
  77.        printf( "b= (%f,%f)\n",b.X(),b.Y() );
  78.        printf( "c= (%f,%f)\n",c.X(),c.Y() );
  79.  
  80.        a = (b + c);
  81.  
  82.        printf("\n\n");
  83.        printf( "a= (%f,%f)\n",a.X(),a.Y() );
  84.        printf( "b= (%f,%f)\n",b.X(),b.Y() );
  85.        printf( "c= (%f,%f)\n",c.X(),c.Y() );
  86.  
  87.        double x,y,z;
  88.        y=z=10.0;
  89.        x=y+z;
  90.        printf("\n\nx=%f,y=%f,z=%f\n",x,y,z); 
  91.    }
  92.  
  93.    Output:
  94.    a=0,0
  95.    b=10,10
  96.    c=10,10
  97.  
  98.    a=0,0
  99.    b=10,10
  100.    c=10,10
  101.  
  102. The base-class operator is not called automatically. So the 'x,y'-part of
  103. the point remains unchanged.  You can easily remove this problem, if you
  104. explicit call the appropriate base-class operator:
  105.  
  106.         XPoint3D operator+( XPoint3D &pt){
  107.             Z() = Z() + pt.Z();
  108.             XPoint :: operator + (pt);
  109.             return *this;
  110.         }
  111.         XPoint3D operator=( XPoint3D &pt){
  112.             Z() = pt.Z();
  113.             XPoint :: operator = (pt);
  114.             return *this;
  115.         }
  116.  
  117.     Enno
  118.